package com.aizhixin.lab.post.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.post.data.SkillPointsData;
import com.aizhixin.lab.post.domain.PostSkillDomain;
import com.aizhixin.lab.post.domain.SkillPointsDomain;
import com.aizhixin.lab.post.server.SkillPointsService;
import com.aizhixin.lab.project.course.domain.StuTaskListDomain;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/post/skillPoints")
@Api(value = "技能点", description = "技能点管理")
public class SkillPointsController {
    @Autowired
    private SkillPointsService pointsService;
    @Autowired
    private AccountService accountService;

    @RequestMapping(value = "/findSkillPointsList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据技能Id获取该技能下的技能点列表", httpMethod = "GET", notes = "根据技能Id获取该技能下的技能点列表<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = SkillPointsData.class)})
    public ResponseEntity<?> findModuleList(@RequestHeader("Authorization") String token, @RequestParam String skillId) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            List<SkillPointsData> skillPoints = pointsService.findSkillPointsList(skillId);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,skillPoints);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/deleteSkillPoints", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除技能点", httpMethod = "DELETE", notes = "删除技能点<br><br><b>lwq</b>")
    public ResponseEntity<?> deleteSkill(@RequestHeader("Authorization") String token, @RequestBody String id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            pointsService.deletePoints(id);
            result.put(ApiReturnConstants.SUCCESS, true);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
        }
        return   new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新增技能点", httpMethod = "POST", notes = "新增技能点<br><br><b>lwq</b>")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token, @RequestBody SkillPointsDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            pointsService.saveDomain(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
        }

        return  new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑", httpMethod = "POST", notes = "编辑<br><br><b>lwq</b>")
    public ResponseEntity<?> update(@RequestHeader("Authorization") String token, @RequestBody SkillPointsDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            pointsService.updateDomain(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
        }

        return  new ResponseEntity(result, HttpStatus.OK);
    }
}
